package com.ruoyi.project.applet.controller;

import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.applet.service.IPayService;
import com.ruoyi.project.system.domain.SysUserWx;
import com.ruoyi.project.system.service.ISysUserWxService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/pay")
public class PayController extends BaseController {


    @Autowired
    IPayService payService;

    @Autowired
    ISysUserWxService sysUserWxService;

    @ApiOperation("会员充值")
    @GetMapping(value = "/pay/{id}")
    public AjaxResult payVip(@PathVariable int id) throws Exception {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx,"请先登录");
        userWx.setVipId(id);
        return payService.pay(userWx);
    }

    @ApiOperation("退款")
    @GetMapping(value = "/vip/refund/{id}")
    public AjaxResult refund(@PathVariable String id,@RequestParam String amount)
    {
        return payService.refund(id,amount,null);
    }

    @ApiOperation("会员充值")
    @GetMapping(value = "/v3/pay/{id}")
    public AjaxResult v3PayVip(@PathVariable int id) throws Exception {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx,"请先登录");
        userWx.setVipId(id);
        return payService.v3Pay(userWx);
    }

    @ApiOperation("支付回调")
    @PostMapping("/payment")
    public String backPayOrder(HttpServletRequest request, HttpServletResponse response) {
        return payService.backPayOrder(request, response);
    }


//    @ApiOperation("提现")
//    @PostMapping(value = "/take")
//    public AjaxResult takePay(@RequestBody Map<String,String> map) throws Exception {
//        LoginUser loginUser = getLoginUser();
//        SysUserWx userWx = loginUser.getUserWx();
//        Assert.notNull(userWx, "请先登录");
//        String amount = map.get("amount");
//        String name = map.get("name");
//        return payService.takePay(amount,name, userWx);
//    }

    @ApiOperation("三方提现")
    @PostMapping(value = "/take")
    public AjaxResult takePay(@RequestBody Map<String,String> map) throws Exception {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx, "请先登录");
        String amount = map.get("amount");
        String openId = map.get("openId");
        return payService.yunGouTakePay(amount,openId, userWx);
    }


    @ApiOperation("提现回调")
    @PostMapping("/take/payment")
    public String backRefundPayOrder(HttpServletRequest request, HttpServletResponse response) {
        return payService.refundPayOrder(request, response);
    }
}
